package cn.tedu.zx.product.webapi.service.iml;


import cn.tedu.zx.pojo.court.dto.AlbumAddNewDto;
import cn.tedu.zx.pojo.court.dto.AlbumUpdateDto;
import cn.tedu.zx.pojo.court.entity.Album;
import cn.tedu.zx.pojo.court.entity.Picture;
import cn.tedu.zx.pojo.court.vo.AlbumDetailsVO;
import cn.tedu.zx.pojo.court.vo.AlbumListVO;
import cn.tedu.zx.pojo.court.vo.PageData;
import cn.tedu.zx.product.service.AlbumService;
import cn.tedu.zx.product.webapi.ex.ServiceCode;
import cn.tedu.zx.product.webapi.ex.ServiceException;
import cn.tedu.zx.product.webapi.mapper.AlbumMapper;
import cn.tedu.zx.product.webapi.mapper.PictureMapper;
import cn.tedu.zx.product.webapi.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AlbumServiceIml implements AlbumService {
    private String message = null;
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public void addNew(AlbumAddNewDto albumAddNewDto) {
        log.debug("往相册添加文件");
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", albumAddNewDto.getName());/*获取传入的名称*/
        int countByName = albumMapper.selectCount(queryWrapper);
        log.debug("查询数据{}",countByName);
        if (countByName>0){
            message = "添加失败，相册名已占用";
            log.warn(message);
//            System.out.println(message);
            throw new ServiceException(ServiceCode.ERR_OCCUPY,message);
        }
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDto,album);
        album.setCreated(LocalDateTime.now());
        album.setUpdated(LocalDateTime.now());
        log.trace("往数据库添加子项：{}",album);
        int insert = albumMapper.insert(album);
        if (insert != 1){
            String message = "服务器忙,m请稍后再试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.trace("{}添加完成",album);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("{}",id);
        QueryWrapper<Album> albumWrapper = new QueryWrapper();
        albumWrapper.eq("id",id);
        int countById = albumMapper.selectCount(albumWrapper);
        if (countById==0){
            message = "删除相册失败，相册不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,message);
        }

        /*相片下是否存在关联数据*/
        QueryWrapper<Picture> pictureWrapper = new QueryWrapper();
        pictureWrapper.eq("album_id",id);
        int countByAlbumId = pictureMapper.selectCount(pictureWrapper);
        if (countByAlbumId>0){
            message = "删除相册失败，相片下存在关联数据";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONTACT,message);
        }


        int delete = albumMapper.deleteById(id);
        if (delete != 1){
            String message = "服务器忙,m请稍后再试";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
    @Override
    public void updateById(AlbumUpdateDto albumUpdateDto) {
        log.debug("{}",albumUpdateDto);
        QueryWrapper<Album> albumWrapper = new QueryWrapper();
        albumWrapper.eq("name",albumUpdateDto.getName()).ne("id",albumUpdateDto.getId());
        int countById = albumMapper.selectCount(albumWrapper);
        if (countById>0){
            message = "修改失败，相册名已占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_OCCUPY,message);
        }
        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateDto,album);
        album.setUpdated(LocalDateTime.now());
        log.trace("往数据库修改子项：{}",album);
        int updateById = albumMapper.updateById(album);
        if (updateById != 1){
            String message = "服务器忙,m请稍后再试";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.trace("{}修改完成",album);
    }

    @Override
    public AlbumDetailsVO selectDetailsById(Long id) {
        log.debug("传入的id：{}",id);
        AlbumDetailsVO albumDetailsVO = albumMapper.selectDetailsById(id);
        log.debug("查到的数据：{}",albumDetailsVO);
        if (albumDetailsVO==null){
            message = "查询失败，根据【id="+id+"】查询的相册详情不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,message);
        }
        return albumDetailsVO;
    }

    @Override
    public PageData list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<AlbumListVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<AlbumListVO> list = albumMapper.listAlbum();
        PageInfo pageInfo = new PageInfo(list);
        return PageInfoToPageDataConverter.converter(pageInfo);
    }


}
